<?php

namespace App\Http\Controllers\CRM\MiniAPP;

use App\Enums\MiniAPP\AssistantReserveStatus;
use App\Helper\ApiResponseHelper;
use App\Http\Controllers\Controller;
use App\Http\Resources\MiniAPP\Fake\AssistantReserveResource;
use App\Models\FakeAssistantReserve;
use Illuminate\Http\Request;

class AssistantReserveController extends Controller
{
    public function getList(Request $request)
    {
        $query = FakeAssistantReserve::latest()->with([
            'assistant.type',
            'member',
            'store'
        ]);

        if (!empty($request->status))
            $query = $query->where('status', $request->status);

        $reserves = $query->paginate();

        return AssistantReserveResource::collection($reserves);
    }

    public function reserveSuccess(FakeAssistantReserve $reserve)
    {
        if ($reserve->status != AssistantReserveStatus::Pending)
            return ApiResponseHelper::json([], 400, '该预约已处理');

        $reserve->status = AssistantReserveStatus::Passed;
        $reserve->save();

        return ApiResponseHelper::json([]);
    }

    public function reserveFail(FakeAssistantReserve $reserve)
    {
        if ($reserve->status != AssistantReserveStatus::Pending)
            return ApiResponseHelper::json([], 400, '该预约已处理');

        $reserve->status = AssistantReserveStatus::Rejected;
        $reserve->save();

        return ApiResponseHelper::json([]);
    }
}
